package com.hbm.usercenter.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.factory.ThreadFactory;
import com.hbm.usercenter.holder.LoadMoreHolder;
import com.hbm.usercenter.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class SuperBaseAdapter<ITEMBEANTYPE> extends BaseAdapter implements
        OnItemClickListener {

    private static final int VIEWTYPE_LOADMORE = 0;                                //
    private static final int VIEWTYPE_NORMAL = 1;                                //

    public List<ITEMBEANTYPE> mDataSource = new ArrayList<ITEMBEANTYPE>();
    private LoadMoreHolder mLoadMoreHolder;
    private LoadMoreTask mLoadMoreTask;
    private AbsListView mAbsListView;

    public SuperBaseAdapter(AbsListView absListView, List<ITEMBEANTYPE> datas) {
        absListView.setOnItemClickListener(this);
        mAbsListView = absListView;
        mDataSource = datas;
    }

    @Override
    public int getCount() {
        if (mDataSource != null) {
            return mDataSource.size() + 1;
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mDataSource != null) {
            return mDataSource.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /*--------------- listview中显示几种布局 begin ---------------*/
    // listview中viewType的总数-->一共有多少种类型
    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getCount() - 1) {
            return VIEWTYPE_LOADMORE;
        }
        return getNormalItemType(position);
    }

    protected int getNormalItemType(int position) {
        return VIEWTYPE_NORMAL;
    }

    /*--------------- listview中显示几种布局 end ---------------*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /*--------------- 视图部分,决定根视图 ---------------*/
        BaseHolder<ITEMBEANTYPE> baseHolder = null;
        if (convertView == null) {
            if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
                baseHolder = (BaseHolder<ITEMBEANTYPE>) getLoadMoreHolder();
            } else {
                baseHolder = getSpecialHolder(position);
            }

        } else {
            baseHolder = (BaseHolder) convertView.getTag();
        }
		/*--------------- 数据展示部分 ---------------*/

        if (getItemViewType(position) == VIEWTYPE_LOADMORE) {// 显示加载更多的holder
            // mLoadMoreHolder.setDataAndRefreshHolderView(LoadMoreHolder.STATE_LOADING);
            if (hasLoadMore()) {
                performLoadMore();
            } else {
                mLoadMoreHolder.setDataAndRefreshHolderView(LoadMoreHolder.STATE_EMPTY);
            }

        } else {// 其他holder
            ITEMBEANTYPE data = mDataSource.get(position);
            baseHolder.setDataAndRefreshHolderView(data);
        }
        return baseHolder.mHolderView;
    }

	/*--------------- 加载更多的逻辑 begin ---------------*/

    private boolean hasLoadMore() {
        return true;
    }

    private void performLoadMore() {
        int state = LoadMoreHolder.STATE_LOADING;
        mLoadMoreHolder.setDataAndRefreshHolderView(state);
        mLoadMoreTask = new LoadMoreTask();
        ThreadFactory.getNormalPool().execute(mLoadMoreTask);
    }

    class LoadMoreTask implements Runnable {

        @Override
        public void run() {
            // 正在的去加载数据-->
			/*--------------- 更加加载更多的数据,处理LoadMoreHolder的一个状态  begin---------------*/
            int state = LoadMoreHolder.STATE_LOADING;
            List<ITEMBEANTYPE> laodMoreDate = null;
            try {
                laodMoreDate = onLoadMoer();
                if (laodMoreDate == null) {
                    state = LoadMoreHolder.STATE_EMPTY;
                } else {
                    if (laodMoreDate.size() < Constants.PAGERSIZE) {
                        state = LoadMoreHolder.STATE_EMPTY;
                    } else {
                        state = LoadMoreHolder.STATE_LOADING;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                state = LoadMoreHolder.STATE_RETRY;
            }
			/*--------------- 更加加载更多的数据,处理LoadMoreHolder的一个状态  end---------------*/

			/*--------------- 定义临时变量  begin ---------------*/
            final int tempState = state;
            final List<ITEMBEANTYPE> tempLoadMoreData = laodMoreDate;
			/*--------------- 定义临时变量  begin ---------------*/
            // 刷新UI-->
            UIUtils.postTaskSafely(new Runnable() {
                @Override
                public void run() {
                    // 会去刷新loadmore视图-->
                    mLoadMoreHolder.setDataAndRefreshHolderView(tempState);
                    // 会去刷新adapter
                    // 修改数据源
                    if (tempLoadMoreData != null && tempLoadMoreData.size() != 0) {
                        mDataSource.addAll(tempLoadMoreData);
                        notifyDataSetChanged();
                    }
                }
            });
            mLoadMoreTask = null;
        }

    }

    private LoadMoreHolder getLoadMoreHolder() {
        if (mLoadMoreHolder == null) {
            mLoadMoreHolder = new LoadMoreHolder();
        }
        return mLoadMoreHolder;

    }

    public List<ITEMBEANTYPE> onLoadMoer() throws Exception {
        return null;
    }

    protected abstract BaseHolder<ITEMBEANTYPE> getSpecialHolder(int position);

    /*--------------- 处理item的点击事件 ---------------*/
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mAbsListView instanceof ListView) {
            ListView listView = (ListView) mAbsListView;
            position = position - listView.getHeaderViewsCount();
        }
        if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
            performLoadMore();
        } else {
            onNormalItemClick(parent, view, position, id);
        }
    }

    /**
     * @des 点击普通的itemView, 空方法
     * @call 如果子类需要处理itemView的点击事件的时候, 覆写该方法, 修改行为
     */
    public void onNormalItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

}
